package google.entry.update;

import db.dao.BaseDao;
import domain.task.Task21;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhongwei on 2017/1/23.
 */
public class UpdateTasks extends Thread {

    @Override
    public void run() {
        BaseDao baseDao = new BaseDao();
        int total = baseDao.getTotalCount("from Task21 where period is null");
        int time = total / 100000;
        Pattern pattern = Pattern.compile("cd_min:(\\d{4})/(\\d{1,2})/(\\d{1,2}),cd_max:(\\d{4})/(\\d{1,2})/(\\d{1,2})");
        int j = 1;
        for (int i = 0; i <= time + 2; i++) {
            List<Task21> toUpdate = baseDao.findWithLimit("from Task21 where period is null", 100000, i * 100000);
            for (Task21 task21 : toUpdate) {
                Matcher matcher = pattern.matcher(task21.getUrl());
                if (matcher.find()) {
                    String startYear = matcher.group(1);
                    String startMonth = matcher.group(2);
                    String startDay = matcher.group(3);
                    String endYear = matcher.group(4);
                    String endMonth = matcher.group(5);
                    String endDay = matcher.group(6);
                    String period = startYear + "-" + startMonth + "-" + startDay + "/" + endYear + "-" + endMonth + "-" + endDay;
                    task21.setPeriod(period);
                    System.out.println("task21 update " + j++);
                    baseDao.update(task21);
                } else {
//                    System.exit(-1);
                }
            }
        }
    }
}
